$(function () {
    window.startDate = $('#datePicker').find('.start').html();
    window.roomItemType = {};
    $('#filterRoomMenu li').each(function (index, item) {
        var liItem = $(item);
        var value = liItem.data('value');
        var name = liItem.data('name');
        var typeValue = liItem.data('type-value');
        var price = liItem.data('price');
        window.roomItemType[value] = {
            'value': value,
            'name': name,
            'type': typeValue,
            'price': price,
            'from': liItem.data('from')
        };
    });
    $('#filterHeader').on('click', '.dropdown-menu li>a', function (e) {
        var item = $(this);
        var liItem = item.parents('li');
        var toggle = liItem.parents('.dropdown-menu').siblings('[data-toggle="dropdown"]');
        liItem.addClass('active');
        liItem.siblings().removeClass('active');
        var value = liItem.data('value');
        var name = item.text();
        if (value === undefined) {
            toggle.data('value', '');
        } else {
            toggle.data('value', value);
        }
        toggle.find('.info').html(name);
    });
    // 查询筛选
    $('#filterSearch').on('click', function () {
        var dateStatus = $('#filterDateType').data('value');
        var filterRoom = $('#filterRoom').data('value');
        var filterChannel = $('#filterChannel').data('value');
        var filterOrderStatus = $('#filterOrderStatus').data('value');
        var filterName = $('#filterName').val();
        var startDate = $('#datePicker').find('.start').html();
        var endDate = $('#datePicker').find('.end').html();
        if (window["context"] == undefined) {
            if (!window.location.origin) {
                window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
            }
            window["context"] = location.origin+"/V6.0";
        }        
        var newHref = window.location.origin + window.location.pathname;
        newHref += '?hotel=' + window.currentHotel;
        newHref += '&datestatus=' + dateStatus;
        newHref += '&start=' + startDate;
        newHref += '&end=' + endDate;
        newHref += filterRoom === undefined || filterRoom === '' ? '' : '&room=' + filterRoom;
        newHref += filterChannel === undefined || filterChannel === '' ? '' : '&channel=' + filterChannel;
        newHref += filterOrderStatus === undefined || filterOrderStatus === '' ? '' : '&status=' + filterOrderStatus;
        newHref += filterName === undefined || filterName === '' ? '' : '&name=' + filterName;
        window.location.href = newHref;
    });
    // 选择日期
    var format = 'YYYY-MM-DD';
    $('#datePickerInput').daterangepicker({
        'showDropdowns': true,
        'autoApply': true,
        'dateLimit': {
            'days': 30
        },
        'locale': {
            'format': format,
            'applyLabel': lang('date.ensure'),
            'cancelLabel': lang('date.cancel'),
            'daysOfWeek': [
                lang('week.7'),
                lang('week.1'),
                lang('week.2'),
                lang('week.3'),
                lang('week.4'),
                lang('week.5'),
                lang('week.6')
            ],
            'monthNames': [
                lang('month.1'),
                lang('month.2'),
                lang('month.3'),
                lang('month.4'),
                lang('month.5'),
                lang('month.6'),
                lang('month.7'),
                lang('month.8'),
                lang('month.9'),
                lang('month.10'),
                lang('month.11'),
                lang('month.12')
            ],
            'firstDay': 1
        },
        'linkedCalendars': false,
        'parentEl': 'body',
        'startDate': window.startDate,
        'endDate': $('#datePicker').find('.end').html(),
        'minDate': '2000-01-01',
        'maxDate': '2038-01-01',
    }, function (start, end, label) {
        window.startDate = start.format(format);
        $('#datePicker').find('.start').html(window.startDate);
        $('#datePicker').find('.end').html(end.format(format));
    });
    $('#datePicker').on('click', function () {
        $('#datePickerInput').trigger('focus');
    });
    var reqTimestamp = (new Date()).valueOf();
    $.getScript("/Public/jslib/lodop/lodop.min.js?" + reqTimestamp);    
});
